草庐IT

Android JSONObject 与 GSON

全部标签

java - 将 Realm 与 Gson 一起使用

我有json字段_idStringjson="{_id:1,name:'Alex',role:'admin'}"在我的Realm模型中,我使用@SerializedName属性:publicclassUserextendsRealmObject{@SerializedName("_id")@PrimaryKeyprivateintid;privateStringname;privateStringcomment;publicintgetId(){returnid;}publicvoidsetId(intid){this.id=id;}publicStringgetName(){retu

android - Retrofit + RealmList + Gson 陷入循环直到内存不足

我正在尝试使用Retrofit+Realm+Gson,但是当使用RealmList时,应用程序卡住了。如果我删除RealmList对象一切正常,但我需要对象列表。日志:(32099):BackgroundstickyconcurrentmarksweepGCfreed278516(15MB)AllocSpaceobjects,0(0B)LOSobjects,25%free,23MB/31MB,paused2.533mstotal1.049s(32099):BackgroundpartialconcurrentmarksweepGCfreed137132(8MB)AllocSpaceob

android - Gson序列化HashMap<Teacher, List<Student>>

我有一个map,其中一个键的值是一个对象列表。能够通过builder.enableComplexMapKeySerialization();序列化key但是这些值没有按预期序列化,因为它们在反序列化时返回字符串而不是对象。下面是序列化的输出[{"id":31001,"name":Teacher"]},//Thisisthekey[{"id":33033,"name":"student1"},{"id":34001,"name":"student2"}]],//Thisisthelistofvalues我使用了相关的TypeToken,它是TypeToken>>但列表值仍然在反序列化时返

android - 使用 Retrofit 获取错误 onFailure : com. google.gson.JsonSyntaxException : java. lang.IllegalStateException: closed

我在将OkHttpClient对象添加到改造中时遇到错误。错误是:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:closed这是我的代码:publicstaticOkHttpClientgetUnsafeOkHttpClient(){try{FilemFolder=newFile(Environment.getExternalStorageDirectory()+"/certificate");if(!mFolder.exists()){mFolder.mkdir();}StringfileNam

android - 使用 GSON 库从 URL(RESTful web 服务)解析 JSON 字符串。安卓

阅读之前:我在这个程序中使用了可下载的GSON库。http://webscripts.softpedia.com/script/Development-Scripts-js/Other-Libraries/Gson-71373.html我尝试解析JSON已经有一段时间了,但每次我尝试从URL中获取字符串时,程序都“不起作用”。它不会失败或关闭或出现错误。它只是不做解析。我的程序旨在从http://api.geonames.org/weatherIcaoJSON?ICAO=LSZH&username=demo解析并有一个按钮来更新再次运行解析过程,以便它会刷新信息。如果我使用硬编码的JS

java - RealmList 序列化问题(Realm/Gson/Intent)

我在项目中使用Retrofit、Gson和Realm。我有这个类Example需要Serializable。如果没有Realm,我会这样写:publicclassExampleimplementsSerializable{@SerializationName("users")privateListusers//...gettersandsetters}Realm开始发挥作用,Example变成(请注意,出于兼容性原因,getter和setter是这种方式):publicclassExampleextendsRealmObjectimplementSerializable{@Serial

java - GSON 避免 JsonSyntaxException 返回部分映射

我遇到了这个问题,我不想解决那个问题,但想办法告诉GSON“跳过错误并继续”解析:Can'tparsesjson:java.lang.IllegalStateException:ExpectedastringbutwasBEGIN_OBJECTatline1column16412使用的代码:JsonReaderreader=newJsonReader(newStringReader(data));reader.setLenient(true);Articlesarticles=gson.create().fromJson(reader,Articles.class);数据是(为了简化)

android - 同一字段有两种不同类型给 Retrofit 2 的 Gson 转换器带来麻烦

这是json模式:如您所见,rated可以是bool值和对象。我正在使用Retrofit2和Gson转换器。我应该如何为这个模式创建我的模型? 最佳答案 这是我解决这个问题的方法:在您的模型中创建自定义类型适配器并手动解析评级;publicclassAccountState{//@SerializedName("rated")//NOPE,parseitmanuallyprivateIntegermRated;//alsodon'tnameitratedpublicIntegergetRated(){returnmRated;}pu

java - 我如何在 Retrofit 库中使用 Gson?

我使用Retrofit发送请求并在android中接收响应,但是当我想要转换来自服务器的响应时遇到问题,它总是给我Exception:retrofit.RetrofitError:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:ExpectedBEGIN_OBJECTbutwasSTRING当服务器的响应应该给我电影列表时,我需要将所有这些电影放入列表中。电影(模型类):publicclassMovie{publicMovie(){}@SerializedName("original_title")

java - GSON。如何将json对象转换为json数组?

现在我从API获取这个JSON:{"supplyPrice":{"CAD":78,"CHF":54600.78,"USD":20735.52}}但是价格是动态的,这就是为什么我需要这种形式的JSON{"supplyPrice":[{"name":"CAD","value":"78"},{"name":"TRY","value":"34961.94"},{"name":"CHF","value":"54600.78"},{"name":"USD","value":"20735.52"}]}如何使用GSON做到这一点? 最佳答案 希望这